﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace homeworkhero
{
    public class Player : CollidableSprite
    {
        private const float VIBRATE_DURATION = 500.0f;
        private PlayerIndex playerIndex;
        private int millisSinceLastHit = (int)VIBRATE_DURATION + 1;

        public Player(Stage s, AnimationSet animSet, PlayerIndex playerIndex)
            : base(s, CollidableSprite.CollidableSpriteType.PLAYER, animSet)
        {
            this.health = 100;
            this.playerIndex = playerIndex;
        }

        public override int getDamage() {
            return 4;
        }

        public override Vector2 getScreenPosition() {
            Vector2 realPos = getPosition();
            // realPos.X += (5.0f - realPos.Y) * Game1.LAYEROFFSETX;
            realPos.X = Game1.SCREENWIDTH / 3.0f - stage.getPlayerX() + getPosition().X;
            realPos.Y = Game1.SCREENHEIGHT - (realPos.Y * Game1.LAYERHEIGHT);
            return realPos;
        }

        public void hit() {
            millisSinceLastHit = 0;
        }

        public override void update(GameTime t)
        {
            if (millisSinceLastHit <= VIBRATE_DURATION)
            {
                float vibration = 1.0f - (float)millisSinceLastHit / VIBRATE_DURATION;
                GamePad.SetVibration(playerIndex, vibration, vibration);
                millisSinceLastHit += t.ElapsedRealTime.Milliseconds;
            } else {
                GamePad.SetVibration(playerIndex, 0.0f, 0.0f);
            }

            base.update(t);
        }
    }
}
